<?php	
	require_once KLEO_LIB_DIR.'core.lib.php';

	class search extends searchmodel{
		/* Call to the model Class */
		function __construct(){
			$this->index();
		}
		function search_box(&$buff){
			$this->tpl_load('form.tpl', $search_form);
			if(isset($_GET['q'])){
				$q = htmlentities($this->escape_query_string(utf8_decode($_GET['q'])));
				$this->data['breadcrumb'] = $this->data['breadcrumb'].' » <a href="?module=search&q='.$q.'">'.$q.'</a>';
				switch($_GET['order']){
					case 'rel':
					$order = 'relevance DESC';
					break;
					case 'id':
					$order = KLEO_TABLE_PREFIX.'blog.id DESC';
					break;
					default:
					$order = 'relevance DESC';
					break;
				}
				die(var_dump("SELECT *, MATCH(title,tagname,content) AGAINST ('%$q%' IN BOOLEAN MODE) AS relevance FROM ".KLEO_TABLE_PREFIX."blog, ".KLEO_TABLE_PREFIX."blog_types, ".KLEO_TABLE_PREFIX."topics, ".KLEO_TABLE_PREFIX."tags, ".KLEO_TABLE_PREFIX."assoc WHERE ".KLEO_TABLE_PREFIX."tags.tagname LIKE '%$q%' AND ".KLEO_TABLE_PREFIX."blog.status = 1 AND ".KLEO_TABLE_PREFIX."topics.tstatus = 1 AND ".KLEO_TABLE_PREFIX."assoc.associtem = ".KLEO_TABLE_PREFIX."blog.id AND ".KLEO_TABLE_PREFIX."tags.tagid = ".KLEO_TABLE_PREFIX."assoc.assoctopic AND ".KLEO_TABLE_PREFIX."assoc.assoctype = 4 AND ".KLEO_TABLE_PREFIX."blog.type = ".KLEO_TABLE_PREFIX."blog_types.tyid GROUP BY  ".KLEO_TABLE_PREFIX."assoc.associtem ORDER BY $order"));

				$this->set_title(L_SEARCH.' | '.$q, $buff, $buff);
				if($this->num_rows($query) !== 0){
					while($row = $this->fetch_array($query)){
						$this->tpl_load('results.tpl', $sbuff);
						$this->tpl_set(array("SEARCH_TITLE" => $row['title'], "SEARCH_LINK" => '?p='.$row['id'], "SEARCH_CONTENT" => substr($row['content'],0,255).'...', "SEARCH_TAGS" => $this->get_the_tags($row['id'])), false, $sbuff,$sbuff);
						$soutput .= $sbuff;
					}
				} else {
					$sbuff = '<br/>'.L_NO_RESULTS;
				}
			}
			$this->tpl_set("BREADCRUMB", $this->data['breadcrumb'], $sbuff, $buff);
			$search_output = $search_form.$soutput;
			$this->tpl_set("WEB_CONTENT", $search_output, $buff, $buff);
			$this->compile($sbuff, $buff);
		}
	}
?>
